package com.dongnao.demo.database;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dongnao.demo.database.bean.Person;
import com.dongnao.demo.database.bean.User;
import com.dongnao.demo.database.db.BaseDao;
import com.dongnao.demo.database.db.BaseDaoFactory;

import java.util.List;
import java.util.Random;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private TextView textView;
    private BaseDao<User> userDao;
    private BaseDao<Person> personDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text_view);
        userDao = BaseDaoFactory.getInstance().getBaseDao(User.class);
        personDao = BaseDaoFactory.getInstance().getBaseDao(Person.class);
    }

    public void insertClick(View view) {
        long zhangsan = userDao.insert(new User(UUID.randomUUID().toString().replaceAll("-", ""), "zhangsan", "123456"));
//        long lisi = userDao.insert(new User(UUID.randomUUID().toString(), "wangwu", "654321"));
        Person p = new Person();
        p.setName("zhangyugehu");
        p.setAge(27);
        personDao.insert(p);
        Toast.makeText(this, "insert ", Toast.LENGTH_SHORT).show();
    }

    public void deleteClick(View view) {
        User where = new User();
        where.setUserName("zhangsan");
        int delete = userDao.delete(where);
        Toast.makeText(this, "delete count " + delete, Toast.LENGTH_SHORT).show();
    }

    public void updateClick(View view) {
        User entity = new User();
        entity.setPassword("234567");
        entity.setUserName("lisi");
        User where = new User();
        where.setUserName("zhangsan");
        long update = userDao.update(entity, where);
        Toast.makeText(this, "update count " + update, Toast.LENGTH_SHORT).show();
    }

    public void queryClick(View view) {
        List<User> users = userDao.queryAll();
        Log.d(TAG, "queryClick: " + users.toString());
        StringBuffer formatText = new StringBuffer("Query Results: \n");
        formatText.append("+++++++++++++++++++++++++++++++++ \n");
        for(User user:users){
            formatText.append("id: ").append(user.getId()).append("\n");
            formatText.append("userName: ").append(user.getUserName()).append("\n");
            formatText.append("password: ").append(user.getPassword()).append("\n");
            formatText.append("---------------------------------- \n");
        }
        formatText.append(personDao.queryAll());
        textView.setText(formatText);
    }
}
